package com.kun.portal.webframe.controller;

import com.kun.portal.webframe.dao.MaintenancePlanDao;
import com.kun.portal.webframe.dao.MaintenancePlanSettingDao;
import com.kun.portal.webframe.dao.MaintenanceRecordDao;
import com.kun.portal.webframe.dao.RepairPlaneSettingDao;
import com.kun.portal.webframe.po.MaintenancePlanPo;
import com.kun.portal.webframe.po.MaintenanceRecordPo;
import com.kun.portal.webframe.service.MaintenancePlanService;
import com.kun.portal.webframe.service.MaintenanceRecordService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * Created by Administrator on 2018/7/13.
 */
@Controller

///api/maintenancePlan/query

@RequestMapping({"/api/maintenanceRecord"})
public class MaintenanceRecordController {
    private static final Logger LOG = LoggerFactory.getLogger(FwUserController.class);


    @Autowired
    private MaintenanceRecordDao maintenanceRecordDao;
    @Autowired
    private MaintenanceRecordService maintenanceRecordService;
    @InitBinder
    public void intDate(WebDataBinder dataBinder){
        dataBinder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
    }
    @RequestMapping(
            value = {"/delete"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public void delete(@RequestBody MaintenanceRecordPo maintenancePlanPo, HttpSession session) {
        maintenanceRecordDao.delete(maintenancePlanPo);
    }

    @RequestMapping(
            value = {"/save"},
            method = {RequestMethod.POST},
            produces = {"application/json"}
    )
    @ResponseBody
    public MaintenanceRecordPo save(@RequestBody MaintenanceRecordPo maintenanceRecordPo, HttpSession session) {
        if(maintenanceRecordPo.getEquipmentPo()!=null){
            if(StringUtils.isBlank(maintenanceRecordPo.getEquipmentPo().getId())){
                maintenanceRecordPo.setEquipmentPo(null);
            }
        }
        if(maintenanceRecordPo.getFlightInfoPo()!=null){
            if(StringUtils.isBlank(maintenanceRecordPo.getFlightInfoPo().getFlightInfoId())){
                maintenanceRecordPo.setFlightInfoPo(null);
            }
        }
        return maintenanceRecordDao.save(maintenanceRecordPo);
    }

    @RequestMapping(
            value = {"/query"},
            method = {RequestMethod.GET},
            produces = {"application/json"}
    )
    @ResponseBody
    public Page<MaintenanceRecordPo> query(@RequestParam (required = false) String filter, Pageable pageable, Boolean isFlight, Date startDate , Date endDate, HttpSession session) {
        return maintenanceRecordService.find(filter,isFlight,startDate,endDate,pageable);
    }

}


